import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  scrollBehavior(to, from, savedPosition) {
    // 如果有保存的位置（浏览器前进/后退），则恢复到该位置
    if (savedPosition) {
      return savedPosition
    }
    // 否则滚动到页面顶部
    return { top: 0 }
  },
  routes: [
    {
      path: '/',
      redirect: '/home/question'
    },
    {
      path: '/home',
      name: 'home',
      redirect: '/home/question',
      component: () => import('@/views/Home.vue'),
      children: [
        {
          path: 'question',
          name: 'question',
          component: () => import('@/views/Question.vue'),
          meta: { showBanner: true }
        },
        {
          path: 'exam',
          name: 'exam',
          component: () => import('@/views/Exam.vue'),
          meta: { showBanner: true }
        },
        {
          path: 'user/exam',
          name: 'userExam',
          component: () => import('@/views/UserExam.vue'),
          meta: { showBanner: false }
        },
        {
          path: 'user/message',
          name: 'userMessage',
          component: () => import('@/views/UserMessage.vue'),
          meta: { showBanner: false }
        },
        {
          path: 'user/detail',
          name: 'userDetail',
          component: () => import('@/views/user/UserDetailLayout.vue'),
          meta: { showBanner: false },
          redirect: '/home/user/detail/info',
          children: [
            {
              path: 'info',
              name: 'userInfo',
              component: () => import('@/views/user/UserInfo.vue')
            },
            {
              path: 'favorite',
              name: 'userFavorite',
              component: () => import('@/views/user/UserFavorite.vue')
            }
          ]
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/Login.vue')
    },
    {
      path: '/anwser',
      name: 'anwser',
      component: () => import('@/views/Answer.vue')
    }
  ]
})

export default router
